package spring.boot.rocketmq.send.produce;

import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import spring.boot.rocketmq.send.config.RocketMQConfig;

/**
 * @author liuli
 * 这个producer一个程序里面只能出现一个
 */
@Log4j2
@Configuration
public class TransactionProducer {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RocketMQConfig rocketMQConfig;
    @Bean
    public TransactionMQProducer defaultProducer() throws MQClientException {
        logger.info("TransactionProduce： {} start ======",rocketMQConfig.toString());
        TransactionMQProducer transactionMQProducer = new TransactionMQProducer(rocketMQConfig.getTransactionGroupId());
        transactionMQProducer.setNamesrvAddr(rocketMQConfig.getNamesrvAddr());
        transactionMQProducer.setVipChannelEnabled(false);
        transactionMQProducer.start();
        logger.info("TransactionProduce： {} end ======",rocketMQConfig.getTransactionGroupId());
        return transactionMQProducer;
    }
}